// Copyright 2019 The rhyme_lph Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// a [author] rhyme_lph
// S [email]  rhymelph@gmail.com
// E [github] https://github.com/rhymelph

import 'package:base_plugin/base_plugin.dart';
import 'package:finance_app/generated/a.dart';
import 'package:finance_app/generated/l10n.dart';
import 'package:finance_app/src/common/config.dart';
import 'package:finance_app/src/page/hr_payroll/views/employee_view.dart';
import 'package:finance_app/src/page/hr_payroll/views/leave_view.dart';
import 'package:finance_app/src/page/hr_payroll/views/payroll_view.dart';
import 'package:finance_app/src/page/hr_payroll/views/reimburse_view.dart';
import 'package:finance_app/src/provider/hr_payroll/hr_and_payroll_provider.dart';
import 'package:finance_app/src/provider/hr_payroll/views/leave_provider.dart';
import 'package:finance_app/src/router_gen/router_meta.dart';
import 'package:finance_app/src/utils/permission_utils.dart';
import 'package:finance_app/src/widgets/button/assistant_button.dart';
import 'package:finance_app/src/widgets/button/normal_float_svg_button.dart';
import 'package:finance_app/src/widgets/button/normal_popup_button.dart';
import 'package:finance_app/src/widgets/common_app_bar.dart';
import 'package:finance_app/src/widgets/common_tab_bar.dart';
import 'package:flutter/material.dart';

@RouterMeta(paramName: "hrAndPayrollPage")
class HrAndPayrollPage extends StatefulWidget {
  final int initIndex;
  final String employeeStatus;
  final bool isSelectEmployee;

  const HrAndPayrollPage(
      {Key key, this.initIndex, this.isSelectEmployee, this.employeeStatus})
      : super(key: key);

  @override
  _HrAndPayrollPageState createState() => _HrAndPayrollPageState();
}

class _HrAndPayrollPageState extends State<HrAndPayrollPage>
    with
        PagePresenter<HrAndPayrollPage, HrAndPayrollProvider>,
        TickerProviderStateMixin {
  TabController tabController;

  @override
  void initState() {
    tabController = TabController(
        length: 4, vsync: this, initialIndex: widget.initIndex ?? 0)
      ..addListener(() {
        provider.onChangeTab(tabController);
      });
    provider.currentIndex = widget.initIndex ?? 0;
    provider.tabController = tabController;
    WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
      provider.onSelectMenuChange(provider.currentIndex, true);
      if (widget.isSelectEmployee == true) {
        provider.getChildProvider<LeaveProvider>().onChangeEmployee();
      }
    });
    super.initState();
  }

  @override
  void dispose() {
    tabController.dispose();
    super.dispose();
  }

  @override
  PreferredSizeWidget get appBar => getDefaultAppBar(
        title: S.current.hr_payroll,
        automaticallyImplyLeading: true,
        actions: Config.hasPermission(PermissionUtils.hrPayroll)
            ? [
                AssistantButton(
                  entryPoint: "HR & Payroll",
                ),
                listBuilder<List<String>>(
                  selector: (_, __) => List.from(__.menuList),
                  builder: (BuildContext context, List<String> value,
                          Widget child) =>
                      value.length > 0
                          ? NormalMorePopupButton(
                              horizontalPadding: getDp(15),
                              onSelect: provider.onSelectMenu,
                              menuList: provider.menuList,
                            )
                          : Container(),
                ),
              ]
            : null,
      );

  @override
  Widget get buildWidget => listBuilder<bool>(
        selector: (_, __) => __.buttonBarIsUp,
        builder: (BuildContext context, bool value, Widget child) => Stack(
          children: [
            child,
            _buildBottomButton(value),
            // _buildTop(),
          ],
        ),
        child: _buildRecordList(),
      );

  TabBarView buildTabBarView() =>
      TabBarView(controller: tabController, children: [
        EmployeeView(
          employeeStatus: widget.employeeStatus,
        ),
        PayrollView(),
        ReimburseView(),
        LeaveView(),
      ]);

  Widget _buildRecordList() => ColoredBox(
        color: Colors.white,
        child: Column(
          children: [
            listBuilder<List<String>>(
              selector: (_, __) => List.from(__.hasBadgeList),
              builder:
                  (BuildContext context, List<String> value, Widget child) =>
                      CommonTabBar(
                controller: tabController,
                tabs: provider.tabs
                    .map((e) => MyTab(
                          text: e,
                          hasBadge: value.contains(e),
                        ))
                    .toList(),
              ),
            ),
            Expanded(child: buildTabBarView()),
          ],
        ),
      );

  Widget _buildBottomButton(bool isTop) => AnimatedPositioned(
      duration: Duration(milliseconds: 300),
      left: 0.0,
      right: 0.0,
      bottom: isTop
          ? MediaQuery.of(context).padding.bottom + getDp(40)
          : -getDp(60),
      child: listBuilder<bool>(
        selector: (_, __) => __.addIsShow,
        builder: (BuildContext context, bool value, Widget child) =>
            value == false
                ? Container()
                : Row(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: [
                      NormalFloatSvgButton(
                        onTap: provider.onNavAdd,
                        asset: A.assetsImagesDashboardAdd,
                      ),
                    ],
                  ),
      ));
}
